home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / source.exe / POSIX / RMDIR / GLOB.C next >
C/C++ Source or Header  |  1993-06-23  |  17KB  |  726 lines

  1. /*
  2.  * Copyright (c) 1989 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Guido van Rossum.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #if defined(LIBC_SCCS) && !defined(lint)
  38. static char sccsid[] = "@(#)glob.c    5.12 (Berkeley) 6/24/91";
  39. #endif /* LIBC_SCCS and not lint */
  40.  
  41. /*
  42.  * glob(3) -- a superset of the one defined in POSIX 1003.2.
  43.  *
  44.  * The [!...] convention to negate a range is supported (SysV, Posix, ksh).
  45.  *
  46.  * Optional extra services, controlled by flags not defined by POSIX:
  47.  *
  48.  * GLOB_MAGCHAR:
  49.  *    Set in gl_flags if pattern contained a globbing character.
  50.  *
  51.  * gl_matchc:
  52.  *    Number of matches in the current invocation of glob.
  53.  */
  54.  
  55. #if WIN_NT
  56. #include <sys/stat.h>
  57. #include <dirent.h>
  58. #include <limits.h>
  59. #include <unistd.h>
  60. #include "glob.h"
  61. #else
  62. #include <sys/cdefs.h>
  63. #include <sys/param.h>
  64. #include <sys/stat.h>
  65. #include <dirent.h>
  66. #include <glob.h>
  67. #endif
  68. #include <ctype.h>
  69. #include <errno.h>
  70. #include <string.h>
  71. #include <stdio.h>
  72. #include <stdlib.h>
  73.  
  74. #if WIN_NT
  75. # define MAXPATHLEN    PATH_MAX
  76. #endif
  77. #define    DOLLAR        '$'
  78. #define    DOT        '.'
  79. #define    EOS        '\0'
  80. #define    LBRACKET    '['
  81. #define    NOT        '!'
  82. #define    QUESTION    '?'
  83. #define    QUOTE        '\\'
  84. #define    RANGE        '-'
  85. #define    RBRACKET    ']'
  86. #define    SEP        '/'
  87. #define    STAR        '*'
  88. #define    TILDE        '~'
  89. #define    UNDERSCORE    '_'
  90. #if WIN_NT
  91. # define PROT1        '\''
  92. #endif
  93.  
  94. #define    M_QUOTE        0x8000
  95. #define    M_PROTECT    0x4000
  96. #define    M_MASK        0xFFFF
  97. #define    M_ASCII        0x00FF
  98.  
  99. #define    CHAR(c)        ((c)&M_ASCII)
  100. #define    META(c)        ((c)|M_QUOTE)
  101. #define    M_ALL        META('*')
  102. #define    M_END        META(']')
  103. #define    M_NOT        META('!')
  104. #define    M_ONE        META('?')
  105. #define    M_RNG        META('-')
  106. #define    M_SET        META('[')
  107. #define    ismeta(c)    (((c)&M_QUOTE) != 0)
  108.  
  109. #if WIN_NT
  110. typedef unsigned short Char;
  111. typedef unsigned char u_char;
  112. typedef unsigned int u_int;
  113.  
  114. static int     compare (const void *, const void *);
  115. static void     g_Ctoc (Char *, char *);
  116. # if 0
  117. static int     g_lstat (Char *, struct stat *);
  118. # endif
  119. static DIR    *g_opendir (Char *);
  120. static Char    *g_strchr (Char *, int);
  121. static int     g_stat (Char *, struct stat *);
  122. static int     glob1 (Char *, glob_t *);
  123. static int     glob2 (Char *, Char *, Char *, glob_t *);
  124. static int     glob3 (Char *, Char *, Char *, Char *, glob_t *);
  125. static int     globextend (Char *, glob_t *);
  126. static int     match (Char *, Char *, Char *);
  127. #ifdef DEBUG
  128. static void     qprintf (Char *);
  129. #endif
  130. #else
  131. typedef u_short Char;
  132.  
  133. static int     compare __P((const void *, const void *));
  134. static void     g_Ctoc __P((Char *, char *));
  135. # if 0
  136. static int     g_lstat __P((Char *, struct stat *));
  137. # endif
  138. static DIR    *g_opendir __P((Char *));
  139. static Char    *g_strchr __P((Char *, int));
  140. static int     g_stat __P((Char *, struct stat *));
  141. static int     glob1 __P((Char *, glob_t *));
  142. static int     glob2 __P((Char *, Char *, Char *, glob_t *));
  143. static int     glob3 __P((Char *, Char *, Char *, Char *, glob_t *));
  144. static int     globextend __P((Char *, glob_t *));
  145. static int     match __P((Char *, Char *, Char *));
  146. #ifdef DEBUG
  147. static void     qprintf __P((Char *));
  148. #endif
  149. #endif
  150. #if WIN_NT
  151.  
  152. static int    isquotesep;    /* Should SEP be QUOTE? */
  153. #endif
  154.  
  155. /*
  156.  * The main glob() routine: compiles the pattern (optionally processing
  157.  * quotes), calls glob1() to do the real pattern matching, and finally
  158.  * sorts the list (unless unsorted operation is requested).  Returns 0
  159.  * if things went well, nonzero if errors occurred.  It is not an error
  160.  * to find no matches.
  161.  */
  162. int
  163. #if __STDC__
  164. glob (const char *pattern, int flags, int (*errfunc)(const char *, int), glob_t *pglob)
  165. #else
  166. glob(pattern, flags, errfunc, pglob)
  167.     const char *pattern;
  168.     int flags, (*errfunc) __P((char *, int));
  169.     glob_t *pglob;
  170. #endif
  171. {
  172.     const u_char *compilepat, *patnext;
  173. #if WIN_NT
  174.     int c, err, oldpathc, prot1;
  175. #else
  176.     int c, err, oldpathc;
  177. #endif
  178.     Char *bufnext, *bufend, *compilebuf, *qpatnext, patbuf[MAXPATHLEN+1];
  179.  
  180. #if WIN_NT
  181.     isquotesep = (getppid() == (pid_t) 1); /* is parent CMD.EXE? */
  182.     prot1 = 0;
  183. #endif
  184.     patnext = (u_char *) pattern;
  185.     if (!(flags & GLOB_APPEND)) {
  186.         pglob->gl_pathc = 0;
  187.         pglob->gl_pathv = NULL;
  188.         if (!(flags & GLOB_DOOFFS))
  189.             pglob->gl_offs = 0;
  190.     }
  191.     pglob->gl_flags = flags & ~GLOB_MAGCHAR;
  192.     pglob->gl_errfunc = errfunc;
  193.     oldpathc = (int) pglob->gl_pathc;
  194.     pglob->gl_matchc = 0;
  195.  
  196.     bufnext = patbuf;
  197.     bufend = bufnext + MAXPATHLEN;
  198.     compilebuf = bufnext;
  199.     compilepat = patnext;
  200.     if (!(flags & GLOB_NOESCAPE)) {
  201.         /* Protect the quoted characters. */
  202.         while (bufnext < bufend && (c = *patnext++) != EOS) 
  203. #if WIN_NT
  204.             if (c == PROT1 && (flags & GLOB_SHQUOTE))
  205.                 prot1 ^= 1;
  206.             else if (prot1)
  207.                 *bufnext++ = (Char) (c | M_PROTECT);
  208.             else if (c == QUOTE) {
  209.                 if (isquotesep)
  210.                     *bufnext++ = (Char) SEP;
  211.                 else {
  212.                     if ((c = *patnext++) == EOS) {
  213.                         c = QUOTE;
  214.                         --patnext;
  215.                     }
  216.                     *bufnext++ = (Char) (c | M_PROTECT);
  217.                 }
  218.             }
  219. #else
  220.             if (c == QUOTE) {
  221.                 if ((c = *patnext++) == EOS) {
  222.                     c = QUOTE;
  223.                     --patnext;
  224.                 }
  225.                 *bufnext++ = (Char) (c | M_PROTECT);
  226.             }
  227. #endif
  228.             else
  229.                 *bufnext++ = (Char) c;
  230.     }
  231.     else 
  232.         while (bufnext < bufend && (c = *patnext++) != EOS) 
  233.             *bufnext++ = (Char) c;
  234.     *bufnext = EOS;
  235.  
  236.     bufnext = patbuf;
  237.     qpatnext = patbuf;
  238.     /* We don't need to check for buffer overflow any more. */
  239.     while ((c = *qpatnext++) != EOS) {
  240.         switch (c) {
  241.         case LBRACKET:
  242.             pglob->gl_flags |= GLOB_MAGCHAR;
  243.             c = *qpatnext;
  244.             if (c == NOT)
  245.                 ++qpatnext;
  246.             if (*qpatnext == EOS ||
  247.                 g_strchr(qpatnext+1, RBRACKET) == NULL) {
  248.                 *bufnext++ = LBRACKET;
  249.                 if (c == NOT)
  250.                     --qpatnext;
  251.                 break;
  252.             }
  253.             *bufnext++ = M_SET;
  254.             if (c == NOT)
  255.                 *bufnext++ = M_NOT;
  256.             c = *qpatnext++;
  257.             do {
  258.                 *bufnext++ = (Char) CHAR(c);
  259.                 if (*qpatnext == RANGE &&
  260.                     (c = qpatnext[1]) != RBRACKET) {
  261.                     *bufnext++ = M_RNG;
  262.                     *bufnext++ = (Char) CHAR(c);
  263.                     qpatnext += 2;
  264.                 }
  265.             } while ((c = *qpatnext++) != RBRACKET);
  266.             *bufnext++ = M_END;
  267.             break;
  268.         case QUESTION:
  269.             pglob->gl_flags |= GLOB_MAGCHAR;
  270.             *bufnext++ = M_ONE;
  271.             break;
  272.         case STAR:
  273.             pglob->gl_flags |= GLOB_MAGCHAR;
  274.             *bufnext++ = M_ALL;
  275.             break;
  276.         default:
  277.             *bufnext++ = (Char) CHAR(c);
  278.             break;
  279.         }
  280.     }
  281.     *bufnext = EOS;
  282. #ifdef DEBUG
  283.     qprintf(patbuf);
  284. #endif
  285.  
  286.     if ((err = glob1(patbuf, pglob)) != 0)
  287.         return(err);
  288.  
  289.     if (pglob->gl_matchc == 0 && !(flags & GLOB_NOCHECK))
  290.         return(GLOB_NOMATCH);
  291.  
  292.     if (pglob->gl_pathc == (size_t) oldpathc && (flags & GLOB_NOCHECK)) {
  293. #if WIN_NT
  294.         if (isquotesep || (flags & GLOB_NOESCAPE)) {
  295. #else
  296.         if (flags & GLOB_NOESCAPE) {
  297. #endif
  298.             Char *dp = compilebuf;
  299.             const u_char *sp = compilepat;
  300. #if WIN_NT
  301.             while (*sp != '\0')
  302.                 if (*sp == PROT1 && (flags & GLOB_SHQUOTE))
  303.                     ++sp;
  304.                 else
  305.                     *dp++ = *sp++;
  306. #else
  307.             while ((*dp++ = *sp++) != '\0')
  308.                 ;
  309. #endif
  310. #if WIN_NT && CONVERT_TO_SLASHES
  311.             for (dp = compilebuf; *dp != '\0'; ++dp)
  312.                 if (*dp == QUOTE)
  313.                     *dp = SEP;
  314. #endif
  315.         }
  316.         else {
  317.             /*
  318.              * Copy pattern, interpreting quotes; this is slightly
  319.              * different than the interpretation of quotes above
  320.              * -- which should prevail?
  321.              */
  322.             while (*compilepat != EOS) {
  323. #if WIN_NT
  324.                 if (*compilepat == PROT1 && (flags & GLOB_SHQUOTE)) {
  325.                     ++compilepat;
  326.                     continue;
  327.                 } else
  328. #endif
  329.                 if (*compilepat == QUOTE) {
  330.                     if (*++compilepat == EOS)
  331.                         --compilepat;
  332.                 }
  333.                 *compilebuf++ = (u_char)*compilepat++;
  334.             }
  335.             *compilebuf = EOS;
  336.         }
  337.         return(globextend(patbuf, pglob));
  338.     } else if (!(flags & GLOB_NOSORT)) 
  339.         qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
  340.             pglob->gl_pathc - oldpathc, sizeof(char *), compare);
  341.     return(0);
  342. }
  343.  
  344. static int
  345. #if __STDC__
  346. compare (const void *p, const void *q)
  347. #else
  348. compare(p, q)
  349.     const void *p, *q;
  350. #endif
  351. {
  352.     return(strcmp(*(char **)p, *(char **)q));
  353. }
  354.  
  355. static int
  356. #if __STDC__
  357. glob1 (Char *pattern, glob_t *pglob)
  358. #else
  359. glob1(pattern, pglob)
  360.     Char *pattern;
  361.     glob_t *pglob;
  362. #endif
  363. {
  364.     Char pathbuf[MAXPATHLEN+1];
  365.  
  366.     /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */
  367.     if (*pattern == EOS)
  368.         return(0);
  369.     return(glob2(pathbuf, pathbuf, pattern, pglob));
  370. }
  371.  
  372. /*
  373.  * The functions glob2 and glob3 are mutually recursive; there is one level
  374.  * of recursion for each segment in the pattern that contains one or more
  375.  * meta characters.
  376.  */
  377. static int
  378. #if __STDC__
  379. glob2 (Char *pathbuf, Char *pathend, Char *pattern, glob_t *pglob)
  380. #else
  381. glob2(pathbuf, pathend, pattern, pglob)
  382.     Char *pathbuf, *pathend, *pattern;
  383.     glob_t *pglob;
  384. #endif
  385. {
  386.     struct stat sb;
  387.     Char *p, *q;
  388.     int anymeta;
  389.  
  390.     /*
  391.      * Loop over pattern segments until end of pattern or until
  392.      * segment with meta character found.
  393.      */
  394.     for (anymeta = 0;;) {
  395.         if (*pattern == EOS) {        /* End of pattern? */
  396.             *pathend = EOS;
  397.             if (g_stat(pathbuf, &sb))
  398.                 return(0);
  399.         
  400.             if (((pglob->gl_flags & GLOB_MARK) &&
  401. #if WIN_NT
  402.                 pathend[-1] != SEP) && S_ISDIR(sb.st_mode)) {
  403.                 *pathend++ = SEP;
  404. #else
  405.                 pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
  406.                 || (S_ISLNK(sb.st_mode) &&
  407.                 (g_stat(pathbuf, &sb) == 0) &&
  408.                 S_ISDIR(sb.st_mode)))) {
  409.                 *pathend++ = SEP;
  410. #endif
  411.                 *pathend = EOS;
  412.             }
  413.             ++pglob->gl_matchc;
  414.             return(globextend(pathbuf, pglob));
  415.         }
  416.  
  417.         /* Find end of next segment, copy tentatively to pathend. */
  418.         q = pathend;
  419.         p = pattern;
  420.         while (*p != EOS && *p != SEP) {
  421.             if (ismeta(*p))
  422.                 anymeta = 1;
  423.             *q++ = *p++;
  424.         }
  425.  
  426.         if (!anymeta) {        /* No expansion, do next segment. */
  427.             pathend = q;
  428.             pattern = p;
  429.             while (*pattern == SEP)
  430.                 *pathend++ = *pattern++;
  431.         } else            /* Need expansion, recurse. */
  432.             return(glob3(pathbuf, pathend, pattern, p, pglob));
  433.     }
  434.     /* NOTREACHED */
  435. }
  436.  
  437. static int
  438. #if __STDC__
  439. glob3 (Char *pathbuf, Char *pathend, Char *pattern, Char *restpattern, glob_t *pglob)
  440. #else
  441. glob3(pathbuf, pathend, pattern, restpattern, pglob)
  442.     Char *pathbuf, *pathend, *pattern, *restpattern;
  443.     glob_t *pglob;
  444. #endif
  445. {
  446.     register struct dirent *dp;
  447.     DIR *dirp;
  448.     int err;
  449.  
  450.     *pathend = EOS;
  451.     errno = 0;
  452.         
  453.     if (!(dirp = g_opendir(pathbuf)))
  454.         /* TODO: don't call for ENOENT or ENOTDIR? */
  455.         if (pglob->gl_errfunc &&
  456.             (*pglob->gl_errfunc)((char *) pathbuf, errno) ||
  457.             (pglob->gl_flags & GLOB_ERR))
  458.             return(GLOB_ABORTED);
  459.         else
  460.             return(0);
  461.  
  462.     err = 0;
  463.  
  464.     /* Search directory for matching names. */
  465.     while ((dp = readdir(dirp)) != NULL) {
  466.         register u_char *sc;
  467.         register Char *dc;
  468.  
  469.         /* Initial DOT must be matched literally. */
  470.         if (dp->d_name[0] == DOT && *pattern != DOT)
  471.             continue;
  472.         for (sc = (u_char *) dp->d_name, dc = pathend; 
  473.              (*dc++ = *sc++) != '\0';)
  474.             ;
  475.         if (!match(pathend, pattern, restpattern)) {
  476.             *pathend = EOS;
  477.             continue;
  478.         }
  479.         err = glob2(pathbuf, --dc, restpattern, pglob);
  480.         if (err)
  481.             break;
  482.     }
  483.  
  484.     /* TODO: check error from readdir? */
  485.     (void)closedir(dirp);
  486.     return(err);
  487. }
  488.  
  489.  
  490. /*
  491.  * Extend the gl_pathv member of a glob_t structure to accomodate a new item,
  492.  * add the new item, and update gl_pathc.
  493.  *
  494.  * This assumes the BSD realloc, which only copies the block when its size
  495.  * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic
  496.  * behavior.
  497.  *
  498.  * Return 0 if new item added, error code if memory couldn't be allocated.
  499.  *
  500.  * Invariant of the glob_t structure:
  501.  *    Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and
  502.  *    gl_pathv points to (gl_offs + gl_pathc + 1) items.
  503.  */
  504. static int
  505. #if __STDC__
  506. globextend (Char *path, glob_t *pglob)
  507. #else
  508. globextend(path, pglob)
  509.     Char *path;
  510.     glob_t *pglob;
  511. #endif
  512. {
  513.     register char **pathv;
  514.     register int i;
  515.     u_int newsize;
  516.     char *copy;
  517.     Char *p;
  518.  
  519.     newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
  520.     pathv = (char **)realloc((char *)pglob->gl_pathv, newsize);
  521.     if (pathv == NULL)
  522.         return(GLOB_NOSPACE);
  523.  
  524.     if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
  525.         /* first time around -- clear initial gl_offs items */
  526.         pathv += pglob->gl_offs;
  527.         for (i = pglob->gl_offs; --i >= 0; )
  528.             *--pathv = NULL;
  529.     }
  530.     pglob->gl_pathv = pathv;
  531.  
  532.     for (p = path; *p++;);
  533.     if ((copy = malloc(p - path)) != NULL) {
  534. #if WIN_NT && CONVERT_TO_BACKSLASHES
  535.         char *c;
  536.  
  537.         g_Ctoc(path, copy);
  538.         for (c = copy; *c++;)
  539.             if (isquotesep && *c == SEP)
  540.                 *c = QUOTE;
  541. #else
  542.         g_Ctoc(path, copy);
  543. #endif
  544.         pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
  545.     }
  546.     pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
  547.     return(copy == NULL ? GLOB_NOSPACE : 0);
  548. }
  549.  
  550.  
  551. /*
  552.  * pattern matching function for filenames.  Each occurrence of the *
  553.  * pattern causes a recursion level.
  554.  */
  555. static int
  556. #if __STDC__
  557. match (register Char *name, register Char *pat, register Char *patend)
  558. #else
  559. match(name, pat, patend)
  560.     register Char *name, *pat, *patend;
  561. #endif
  562. {
  563.     int ok, negate_range;
  564.     Char c, k;
  565.  
  566.     while (pat < patend) {
  567.         c = *pat++;
  568.         switch (c & M_MASK) {
  569.         case M_ALL:
  570.             if (pat == patend)
  571.                 return(1);
  572.             for (; *name != EOS; ++name)
  573.                 if (match(name, pat, patend))
  574.                     return(1);
  575.             return(0);
  576.         case M_ONE:
  577.             if (*name++ == EOS)
  578.                 return(0);
  579.             break;
  580.         case M_SET:
  581.             ok = 0;
  582.             k = *name++;
  583.             if ((negate_range = ((*pat & M_MASK) == M_NOT)) != 0)
  584.                 ++pat;
  585.             while (((c = *pat++) & M_MASK) != M_END)
  586.                 if ((*pat & M_MASK) == M_RNG) {
  587.                     if (c <= k && k <= pat[1])
  588.                         ok = 1;
  589.                     pat += 2;
  590.                 } else if (c == k)
  591.                     ok = 1;
  592.             if (ok == negate_range)
  593.                 return(0);
  594.             break;
  595.         default:
  596.             if (*name++ != c)
  597.                 return(0);
  598.             break;
  599.         }
  600.     }
  601.     return(*name == EOS);
  602. }
  603.  
  604. /* Free allocated data belonging to a glob_t structure. */
  605. void
  606. #if __STDC__
  607. globfree (glob_t *pglob)
  608. #else
  609. globfree(pglob)
  610.     glob_t *pglob;
  611. #endif
  612. {
  613.     register int i;
  614.     register char **pp;
  615.  
  616.     if (pglob->gl_pathv != NULL) {
  617.         pp = pglob->gl_pathv + pglob->gl_offs;
  618.         for (i = pglob->gl_pathc; i--; ++pp)
  619.             if (*pp)
  620.                 free(*pp);
  621.         free(pglob->gl_pathv);
  622.     }
  623. }
  624.  
  625. static DIR *
  626. #if __STDC__
  627. g_opendir (register Char *str)
  628. #else
  629. g_opendir(str)
  630.     register Char *str;
  631. #endif
  632. {
  633.     char buf[MAXPATHLEN];
  634.  
  635.     if (!*str)
  636.         return(opendir("."));
  637.     g_Ctoc(str, buf);
  638.     return(opendir(buf));
  639. }
  640. #if 0
  641.  
  642. static int
  643. #if __STDC__
  644. g_lstat (register Char *fn, struct stat *sb)
  645. #else
  646. g_lstat(fn, sb)
  647.     register Char *fn;
  648.     struct stat *sb;
  649. #endif
  650. {
  651.     char buf[MAXPATHLEN];
  652.  
  653.     g_Ctoc(fn, buf);
  654.     return(lstat(buf, sb));
  655. }
  656. #endif
  657.  
  658. static int
  659. #if __STDC__
  660. g_stat (register Char *fn, struct stat *sb)
  661. #else
  662. g_stat(fn, sb)
  663.     register Char *fn;
  664.     struct stat *sb;
  665. #endif
  666. {
  667.     char buf[MAXPATHLEN];
  668.  
  669.     g_Ctoc(fn, buf);
  670.     return(stat(buf, sb));
  671. }
  672.  
  673. static Char *
  674. #if __STDC__
  675. g_strchr (Char *str, int ch)
  676. #else
  677. g_strchr(str, ch)
  678.     Char *str;
  679.     int ch;
  680. #endif
  681. {
  682.     do {
  683.         if (*str == (Char) ch)
  684.             return (str);
  685.     } while (*str++);
  686.     return (NULL);
  687. }
  688.  
  689. static void
  690. #if __STDC__
  691. g_Ctoc (register Char *str, char *buf)
  692. #else
  693. g_Ctoc(str, buf)
  694.     register Char *str;
  695.     char *buf;
  696. #endif
  697. {
  698.     register char *dc;
  699.  
  700.     for (dc = buf; (*dc++ = (char) *str++) != '\0';)
  701.         ;
  702. }
  703.  
  704. #ifdef DEBUG
  705. static void 
  706. #if __STDC__
  707. qprintf (register Char *s)
  708. #else
  709. qprintf(s)
  710.     register Char *s;
  711. #endif
  712. {
  713.     register Char *p;
  714.  
  715.     for (p = s; *p; p++)
  716.         (void)printf("%c", *p & 0xff);
  717.     (void)printf("\n");
  718.     for (p = s; *p; p++)
  719.         (void)printf("%c", *p & M_PROTECT ? '"' : ' ');
  720.     (void)printf("\n");
  721.     for (p = s; *p; p++)
  722.         (void)printf("%c", *p & M_META ? '_' : ' ');
  723.     (void)printf("\n");
  724. }
  725. #endif
  726.